﻿using System.Collections.Generic;
using Iservices;
using Model;
using EFEntity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;
using System.Data;
using System.Threading.Tasks;
using System.Linq;

namespace Services
{
    public class UsersServices : IUsersServices
    {
        private readonly MyDbContext myDbContext;

        public UsersServices(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        public async Task<List<users>> UsersSelectFenYe(FenYe fy)
        {
            SqlParameter[] ps = {
            new SqlParameter(){  ParameterName="@pageSize",Value=fy.PageSize},
            new SqlParameter(){  ParameterName="@keyName",Value="Id"},
            new SqlParameter(){  ParameterName="@tabelName",Value="users"},
            new SqlParameter(){  ParameterName="@where",Value=fy.Where},
            new SqlParameter(){  ParameterName="@currentPage",Value=fy.CurrentPage},
            new SqlParameter(){  ParameterName="@lie",Value="*"},
            new SqlParameter(){  ParameterName="@rows",Direction=ParameterDirection.Output,SqlDbType= SqlDbType.Int},
            new SqlParameter(){  ParameterName="@pages",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int},
            };
           List<Eusers> list=await  myDbContext.users.FromSqlRaw("exec [dbo].[proc_Fenye] @pageSize, @keyName, @tabelName, @where, @currentPage, @lie, @rows out, @pages out",ps).ToListAsync();
            fy.Rows = (int)ps[6].Value;
            fy.Pages = (int)ps[7].Value;
            //把EO-DTO
            List<users> list2 = new List<users>();
            foreach (Eusers item in list)
            {
                users t = new users() { 
                user_id=item.user_id,
                user_name=item.user_name,
                user_true_name=item.user_true_name,
                user_password = item.user_password
                };
                list2.Add(t);
            }
            return list2;
        }

        public async Task<List<users>> UsersSelectFenYe2(FenYe fy)
        {
          var data=  myDbContext.users.OrderBy(e => e.user_id).ToList();
            fy.Rows = data.Count();
            fy.Pages = (fy.Rows - 1) / fy.PageSize + 1;
            List<Eusers> list = new List<Eusers>();
           await Task.Run(() =>
            {
                 list = data.Skip((fy.CurrentPage - 1) * fy.PageSize)
                     .Take(fy.PageSize)
                     .ToList();
            });
            //把EO-DTO
            List<users> list2 = new List<users>();
            foreach (Eusers item in list)
            {
                users t = new users()
                {
                    user_id = item.user_id,
                    user_name = item.user_name,
                    user_true_name = item.user_true_name,
                    user_password = item.user_password,
                  
                };
                list2.Add(t);
            }
            return list2;
        }

    }
}
